#include "apis.hpp"
#include "config.h"
#include "pch.hpp"
#include "sts.hpp"
// ----------

Client *clients;
Display *dpy;
Atom atoms[AtomLast]; // Atom from X11
Parameter *curconfig;
char *stylefile;
GdkDevice *gdkkb;
int spair[2];
char const *useragent;
int cookiepolicy;
char *argv0;
Window embed;

int showxid;
ParamName loadtransient[] = {Certificate, CookiePolicies,      DiskCache,
                             DNSPrefetch, FileURLsCrossAccess, JavaScript,
                             LoadImages,  PreferredLanguages,  ShowIndicators,
                             StrictTLS,   ParameterLast};
ParamName loadcommitted[] = {
    //	AccessMicrophone,
    //	AccessWebcam,
    CaretBrowsing, DarkMode, DefaultCharset, FontSize, Geolocation,
    HideBackground, Inspector,
    //	KioskMode,
    MediaManualPlay, RunInFullscreen, ScrollBars, SiteQuirks, SmoothScrolling,
    SpellChecking, SpellLanguages, Style, ZoomLevel, ParameterLast};
ParamName loadfinished[] = {ParameterLast};
int modparams[ParameterLast];

int main(int argc, char **argv) {
  Arg arg;
  Client *c;
  memset(&arg, 0, sizeof(arg));

  for (argv0 = *argv, argv++, argc--;
       argv[0] && argv[0][0] == '-' && argv[0][1]; argc--, argv++) {
    char argc_;
    char **argv_;
    int brk_;
    if (argv[0][1] == '-' && argv[0][2] == '\0') {
      argv++;
      argc--;
      break;
    }
    for (brk_ = 0, argv[0]++, argv_ = argv; argv[0][0] && !brk_; argv[0]++) {
      if (argv_ != argv) {
        break;
      }
      argc_ = argv[0][0];
      switch (argc_) {
      case 'a':
        defconfig[CookiePolicies].val.v =
            ((argv[0][1] == '\0' && argv[1] == ((void *)0))
                 ? ((usage()), abort(), (char *)0)
                 : (brk_ = 1, (argv[0][1] != '\0')
                                  ? (&argv[0][1])
                                  : (argc--, argv++, argv[0])));
        defconfig[CookiePolicies].prio = 2;
        break;
      case 'b':
        defconfig[ScrollBars].val.i = 0;
        defconfig[ScrollBars].prio = 2;
        break;
      case 'B':
        defconfig[ScrollBars].val.i = 1;
        defconfig[ScrollBars].prio = 2;
        break;
      case 'c':
        cookiefile = ((argv[0][1] == '\0' && argv[1] == ((void *)0))
                          ? ((usage()), abort(), (char *)0)
                          : (brk_ = 1, (argv[0][1] != '\0')
                                           ? (&argv[0][1])
                                           : (argc--, argv++, argv[0])));
        break;
      case 'C':
        stylefile = ((argv[0][1] == '\0' && argv[1] == ((void *)0))
                         ? ((usage()), abort(), (char *)0)
                         : (brk_ = 1, (argv[0][1] != '\0')
                                          ? (&argv[0][1])
                                          : (argc--, argv++, argv[0])));
        break;
      case 'd':
        defconfig[DiskCache].val.i = 0;
        defconfig[DiskCache].prio = 2;
        break;
      case 'D':
        defconfig[DiskCache].val.i = 1;
        defconfig[DiskCache].prio = 2;
        break;
      case 'e':
        embed = strtol(((argv[0][1] == '\0' && argv[1] == ((void *)0))
                            ? ((usage()), abort(), (char *)0)
                            : (brk_ = 1, (argv[0][1] != '\0')
                                             ? (&argv[0][1])
                                             : (argc--, argv++, argv[0]))),
                       NULL, 0);
        break;
      case 'f':
        defconfig[RunInFullscreen].val.i = 0;
        defconfig[RunInFullscreen].prio = 2;
        break;
      case 'F':
        defconfig[RunInFullscreen].val.i = 1;
        defconfig[RunInFullscreen].prio = 2;
        break;
      case 'g':
        defconfig[Geolocation].val.i = 0;
        defconfig[Geolocation].prio = 2;
        break;
      case 'G':
        defconfig[Geolocation].val.i = 1;
        defconfig[Geolocation].prio = 2;
        break;
      case 'i':
        defconfig[LoadImages].val.i = 0;
        defconfig[LoadImages].prio = 2;
        break;
      case 'I':
        defconfig[LoadImages].val.i = 1;
        defconfig[LoadImages].prio = 2;
        break;
      case 'k':
        defconfig[KioskMode].val.i = 0;
        defconfig[KioskMode].prio = 2;
        break;
      case 'K':
        defconfig[KioskMode].val.i = 1;
        defconfig[KioskMode].prio = 2;
        break;
      case 'm':
        defconfig[Style].val.i = 0;
        defconfig[Style].prio = 2;
        break;
      case 'M':
        defconfig[Style].val.i = 1;
        defconfig[Style].prio = 2;
        break;
      case 'n':
        defconfig[Inspector].val.i = 0;
        defconfig[Inspector].prio = 2;
        break;
      case 'N':
        defconfig[Inspector].val.i = 1;
        defconfig[Inspector].prio = 2;
        break;
      case 'r':
        scriptfile = ((argv[0][1] == '\0' && argv[1] == ((void *)0))
                          ? ((usage()), abort(), (char *)0)
                          : (brk_ = 1, (argv[0][1] != '\0')
                                           ? (&argv[0][1])
                                           : (argc--, argv++, argv[0])));
        break;
      case 's':
        defconfig[JavaScript].val.i = 0;
        defconfig[JavaScript].prio = 2;
        break;
      case 'S':
        defconfig[JavaScript].val.i = 1;
        defconfig[JavaScript].prio = 2;
        break;
      case 't':
        defconfig[StrictTLS].val.i = 0;
        defconfig[StrictTLS].prio = 2;
        break;
      case 'T':
        defconfig[StrictTLS].val.i = 1;
        defconfig[StrictTLS].prio = 2;
        break;
      case 'u':
        fulluseragent = ((argv[0][1] == '\0' && argv[1] == ((void *)0))
                             ? ((usage()), abort(), (char *)0)
                             : (brk_ = 1, (argv[0][1] != '\0')
                                              ? (&argv[0][1])
                                              : (argc--, argv++, argv[0])));
        break;
      case 'v':
        die("surf-" VERSION ", see LICENSE for © details\n");
      case 'w':
        showxid = 1;
        break;
      case 'x':
        defconfig[Certificate].val.i = 0;
        defconfig[Certificate].prio = 2;
        break;
      case 'X':
        defconfig[Certificate].val.i = 1;
        defconfig[Certificate].prio = 2;
        break;
      case 'z':
        defconfig[ZoomLevel].val.f =
            strtof(((argv[0][1] == '\0' && argv[1] == ((void *)0))
                        ? ((usage()), abort(), (char *)0)
                        : (brk_ = 1, (argv[0][1] != '\0')
                                         ? (&argv[0][1])
                                         : (argc--, argv++, argv[0]))),
                   NULL);
        defconfig[ZoomLevel].prio = 2;
        break;
      default:
        break; // usage();
      };
    }
  }

  if (argc > 0) {
    arg.v = argv[0];
  } else {
    arg.v = "about:blank";
  }

  setup();
  c = newclient(NULL);
  showview(NULL, c);

  loaduri(c, &arg);
  updatetitle(c);

  gtk_main();
  cleanup();

  return 0;
}
